﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Model;
using TCSmartFramework.DataAccess;

namespace PostSubmit
{
    public partial class InsertOrder : Form
    {
        public InsertOrder()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder stringBuilder=new StringBuilder();


            TCSmartFramework.DataAccess.DatabaseConfigManager.SetConfigPath(AppDomain.CurrentDomain.BaseDirectory + "\\TCSmartFramework.DataAccess.config");
            int orderId = TypeParse.StrToInt(txt_orderId.Text, 0);
            string sql = string.Format("{0} AND Id={1}", FlightOrderModel.GetSelectSQL(), orderId);
            string sqlPassenger = string.Format("{0} AND OrderId={1}", FlightPassengerModel.GetSelectSQL(), orderId);

            Database databaseOnline = DatabaseFactory.GetWriteDatabase(DBHelper.DB_WriteOnLine);
            Database databaseLocalhost = DatabaseFactory.GetWriteDatabase(DBHelper.DB_WriteLocalhost);
            DataTable dataTable = SqlHelper.ExecuteDataTable(databaseOnline, sql);
            //DataTable dataTable = DBHelper.ExecuteDataTable(sql, DBHelper.DB_WriteOnLine);
            try
            {
                if (dataTable != null)
                {
                    FlightOrderModel orderModel = new FlightOrderModel(dataTable.Rows[0]);
                    SqlParameterWrapperCollection sqlParameterWrapperCollectionOrder=new SqlParameterWrapperCollection();
                    SqlParameter[] sqlParametersOrders = orderModel.ToSqlParameters();
                    for (int i = 0; i < sqlParametersOrders.Length; i++)
                    {
                        SqlParameter sqlParameter = sqlParametersOrders[i];
                        sqlParameterWrapperCollectionOrder.Add(new SqlParameterWrapper(sqlParameter.ParameterName,sqlParameter.Value));
                    }


                    int orderIdLocal = SqlHelper.ExecuteIdentity(databaseLocalhost, FlightOrderModel.GetInsertSQL(), sqlParameterWrapperCollectionOrder);
                    stringBuilder.AppendFormat("订单号：{0}<br />", orderIdLocal);
                    DataTable dataTablePasseger = SqlHelper.ExecuteDataTable(databaseOnline, sqlPassenger);
                    foreach (DataRow dataRow in dataTablePasseger.Rows)
                    {
                        FlightPassengerModel passengerModel=new FlightPassengerModel(dataRow);
                        passengerModel.OrderId = orderIdLocal;
                        SqlParameterWrapperCollection sqlParameterWrapperCollectionPassenger = new SqlParameterWrapperCollection();
                        SqlParameter[] sqlParametersPassenger = passengerModel.ToSqlParameters();
                        for (int i = 0; i < sqlParametersPassenger.Length; i++)
                        {
                            SqlParameter sqlParameter = sqlParametersPassenger[i];
                            sqlParameterWrapperCollectionPassenger.Add(new SqlParameterWrapper(sqlParameter.ParameterName, sqlParameter.Value));
                        }
                       int passengerId =  SqlHelper.ExecuteIdentity(databaseLocalhost, FlightPassengerModel.GetInsertSQL(), sqlParameterWrapperCollectionPassenger);
                       stringBuilder.AppendFormat("乘客Id：{0}<br />", passengerId);
                    }
                }
                richTextBox1.Text = stringBuilder.ToString();
            }
            catch (Exception ex)
            {

            }

        }
    }

}
